package com.example.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plan.common.result.Result;
import com.example.plan.exception.BusinessException;
import com.example.plan.model.constnant.DroneEnums;
import com.example.plan.model.dto.riskAlert.RiskAlertDTO;
import com.example.plan.model.dto.riskAlert.RiskAlertPageDTO;
import com.example.plan.model.entity.Drone;
import com.example.plan.model.entity.RiskAlert;
import com.example.plan.model.vo.DroneVO;
import com.example.plan.model.vo.RiskAlertVO;
import com.example.plan.service.RiskAlertService;
import com.example.plan.mapper.RiskAlertMapper;
import com.example.plan.utils.BeanPageUtil;
import org.springframework.stereotype.Service;

import static com.example.plan.exception.ErrorCode.RISK_ALERT_NOT_FOUND;
import static com.example.plan.exception.ErrorCode.UPDATE_RISK_ALERT_ID_IS_NULL;

/**
* @author ASUS
* @description 针对表【risk_alert(运输风险预警表)】的数据库操作Service实现
* @createDate 2025-03-01 19:07:17
*/
@Service
public class RiskAlertServiceImpl extends ServiceImpl<RiskAlertMapper, RiskAlert> implements RiskAlertService{

    /**
     *  新增运输风险预警
     * @param dto
     * @return
     */
    @Override
    public Result<Void> saveRiskAlert(RiskAlertDTO dto) {
        RiskAlert riskAlert= BeanUtil.copyProperties(dto, RiskAlert.class);
        return baseMapper.insert(riskAlert)>0?Result.success("保存成功"):Result.failed("保存失败");
    }

    /**
     *  删除运输风险预警
     * @param alertId
     * @return
     */
    @Override
    public Result<Void> deleteRiskAlert(Long alertId) {
        boolean flag=baseMapper.selectById(alertId)==null||baseMapper.deleteById(alertId)>0;
        return flag?Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     *  查询运输风险预警详情
     * @param alertId
     * @return
     */
    @Override
    public Result<RiskAlertVO> getRiskAlert(Long alertId) {
        RiskAlert riskAlert=baseMapper.selectById(alertId);
        if (riskAlert==null){
            throw new BusinessException(RISK_ALERT_NOT_FOUND);
        }
        RiskAlertVO riskAlertVO=BeanUtil.copyProperties(riskAlert,RiskAlertVO.class);
        return Result.success("运输风险预警详情",riskAlertVO);
    }

    @Override
    public Result<IPage<RiskAlertVO>> pageRiskAlert(RiskAlertPageDTO dto) {
        QueryWrapper<RiskAlert> queryWrapper=new QueryWrapper<>();
        if (dto.getSortOrder()== DroneEnums.sortOrderEnum.ASC.getCode()){
            queryWrapper.orderByAsc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }else {
            queryWrapper.orderByDesc(StrUtil.isNotBlank(dto.getSortField()),dto.getSortField());
        }
        LambdaQueryWrapper<RiskAlert> lambdaQueryWrapper=queryWrapper
                .lambda()
                .like(StrUtil.isNotBlank(dto.getSuggestion()),RiskAlert::getSuggestion,dto.getSuggestion());
        Page<RiskAlert> page = new Page<>(dto.getCurrent(), dto.getPageSize());
        Page<RiskAlert> riskAlert = baseMapper.selectPage(page, lambdaQueryWrapper);
        Page<RiskAlertVO> pagevo = BeanPageUtil.copyBean(riskAlert, RiskAlertVO.class);
        return Result.success(pagevo);
    }

    /**
     *  修改运输风险预警
     * @param dto
     * @return
     */
    @Override
    public Result<Void> updateRiskAlert(RiskAlertDTO dto) {
        if (dto.getAlert_id()==null){
            throw new BusinessException(UPDATE_RISK_ALERT_ID_IS_NULL);
        }
        RiskAlert riskAlert= BeanUtil.copyProperties(dto, RiskAlert.class);
        return baseMapper.updateById(riskAlert)>0?Result.success("修改成功"):Result.failed("修改失败");
    }

}




